/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.verolux.soft.debaraz.utils;

import com.verolux.soft.debaraz.ApplicationException;
import com.verolux.soft.debaraz.bean.Article;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author ut1e8p
 */
@Component(value="articleImageUploader")
public class ArticleImageUploader {

    @Autowired
    private Properties appProperties;
    
    private Article article;

    /**
     *
     */
    public ArticleImageUploader() {
    }


    /**
     * 
     * @param article
     */
    public ArticleImageUploader(Article article) {
        this.article = article;
    }

    /**
     * As article's id is used as directory name, assume that article's
     * id is not null before using this method
     */
    public void upload() throws ApplicationException {
        String rootDir = appProperties.getProperty("upload.root.dir");
        String rootPath = appProperties.getProperty("upload.root.path");

        String shortPath = rootPath + "/articles/" + article.getId();
        String dir = rootDir + "/articles/" + article.getId();

        try {
                if (!article.getMainImageData().isEmpty()) {
                    String path = UploadUtils.buildOutputFilePath(article.getMainImageData().getOriginalFilename(), "1", dir);
                    UploadUtils.writeStream(article.getMainImageData(), path);
                    String dbPath = UploadUtils.buildOutputFilePath(article.getMainImageData().getOriginalFilename(), "1", shortPath);
                    article.setImage(dbPath);
                }

                if (!article.getImageData2().isEmpty()) {
                    String path = UploadUtils.buildOutputFilePath(article.getImageData2().getOriginalFilename(), "2", dir);
                    UploadUtils.writeStream(article.getImageData2(), path);
                    String dbPath = UploadUtils.buildOutputFilePath(article.getMainImageData().getOriginalFilename(), "2", shortPath);
                    article.addImageInfo(dbPath);
                }

                if (!article.getImageData3().isEmpty()) {
                    String path = UploadUtils.buildOutputFilePath(article.getImageData3().getOriginalFilename(), "3", dir);
                    UploadUtils.writeStream(article.getImageData3(), path);
                    String dbPath = UploadUtils.buildOutputFilePath(article.getMainImageData().getOriginalFilename(), "3", shortPath);
                    article.addImageInfo(dbPath);
                }

                if (!article.getImageData4().isEmpty()) {
                    String path = UploadUtils.buildOutputFilePath(article.getImageData4().getOriginalFilename(), "4", dir);
                    UploadUtils.writeStream(article.getImageData4(), path);
                    String dbPath = UploadUtils.buildOutputFilePath(article.getMainImageData().getOriginalFilename(), "4", shortPath);
                    article.addImageInfo(dbPath);
                }
        } catch (Exception e){
            throw new ApplicationException(e.getMessage(), e);
        }
    }

    public Properties getAppProperties() {
        return appProperties;
    }

    public void setAppProperties(Properties appProperties) {
        this.appProperties = appProperties;
    }

    public Article getArticle() {
        return article;
    }

    public void setArticle(Article article) {
        this.article = article;
    }
}
